package com.example.controller;


import com.example.entity.Menu;
import com.example.service.MenuService;
import com.example.utils.ResponseResult;
import com.example.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeselect(@PathVariable Long id){
        return menuService.roleMenuTreeselect(id);
    }

    @GetMapping("list")
    public ResponseResult getList(@PathParam("menuName") String menuName, @PathParam("status") String status){
        return menuService.getList(menuName,status);
    }

    @GetMapping("/{id}")
    public ResponseResult getMenu(@PathVariable Long id){
        return ResponseResult.okResult(menuService.getById(id));
    }

    @PutMapping()
    public ResponseResult updateMenu(@RequestBody Menu menu){
        menu.setUpdateTime(new Date());
        menuService.updateById(menu);
        return ResponseResult.okResult();
    }

    @PostMapping()
    public ResponseResult saveMenu(@RequestBody Menu menu){
        menu.setCreateTime(new Date());
        menu.setCreateBy(SecurityUtils.getUserId());
        menuService.save(menu);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteMenus(@PathVariable Long id){
        return menuService.deleteMenus(id);
    }
}
